<?php 
/**
* @orm tableName forum_posts
* @orm index(datetime)
*/
abstract class ForumPostBase extends DomArObject {
	
	/**
	* @orm plugin DomArDateTime(now)
	*/
	public $datetime;	
	/**
	* @orm text
	*/
	public $title;
	/**
	* @orm plugin DomArImage({autoRename=1r})
	* @orm listener afterAdd afterAddIcon
	*/
	public $icon;
	/**
	* @orm text
	*/
	public $contents;
	
	
	/**
	* @orm has child ForumUser
	*/
	public $user;
	
	
	/**
	* @orm owns many ForumImage inverse post canAddImage()
	* @orm listener afterAdd afterAddImage
	* @orm listener afterRemove afterRemoveImage
	*/
	public $images;
	/**
	* @orm int(11)
	*/
	public $nrOfImages = 0;
	
	/**
	* @orm bool
	*/
	public $sendEmailOnReplay = false;
	
	/**
	* @orm owns many ForumImage canAddImage()
	* @orm listener afterAdd afterAddImage
	* @orm listener afterRemove afterRemoveImage
	*/
	public $images;
	
	public function getValidationErrorMessages() {
		return array(
			'images' => array(
				1000 => 'Piltide lisamine ei ole antud teemasse lubatud!',
			),
		);
	}
	
	/**
	* @return Int [0 on successs, 1000 thread doesnt allow images]
	*/
	public function canAddImage( $image ) {
		if( get_class($this) == 'ForumTopic' )
			$parent = $this->thread;
		else
			$parent = $this->topic->thread;
		
		if( $parent->allowImages == false ) {
			return 1000;
		}
		return 0;
	}

	public function afterAddImage() {
		$this->nrOfImages++;
	}
	
	public function afterRemoveImage() {
		$this->nrOfImages--;
	}
	
	public function afterAddIcon() {
		if( $this->icon->exists() ) {
			$this->icon->resize(80,50, false);
			$this->icon->cut(80,50);
		}
	}
	
	public static function parsePost( &$entry ) {
		$repl = array();
		
		$repl[] = array('tag'=>':vihane:', 'image'=>'angry.gif');
		$repl[] = array('tag'=>':naeratav:', 'image'=>'cheerful.gif');
		$repl[] = array('tag'=>'8)', 'image'=>'cool.gif');
		$repl[] = array('tag'=>':nutab:', 'image'=>'cry.gif');
		$repl[] = array('tag'=>':puiklev:', 'image'=>'devious.gif');
		$repl[] = array('tag'=>':S', 'image'=>'ermm.gif');
		$repl[] = array('tag'=>':kuri:', 'image'=>'evil.gif');
		$repl[] = array('tag'=>':irve:', 'image'=>'grin.gif');
		$repl[] = array('tag'=>':)', 'image'=>'happy.gif');
		$repl[] = array('tag'=>':D', 'image'=>'laugh.gif');
		$repl[] = array('tag'=>':(', 'image'=>'sad.gif');
		$repl[] = array('tag'=>':p', 'image'=>'tounge.gif');
		$repl[] = array('tag'=>';)', 'image'=>'wink.gif');
		$repl[] = array('tag'=>"\[b\]([^\[]+)\[/b\]", 'tagReplace'=>"<b>\\1</b>");
		$repl[] = array('tag'=>"\[i\]([^\[]+)\[/i\]", 'tagReplace'=>"<i>\\1</i>");
		$repl[] = array('tag'=>"\[u\]([^\[]+)\[/u\]", 'tagReplace'=>"<u>\\1</u>");
		$repl[] = array('tag'=>"\[img\]([^\[]+)\[/img\]", 'tagReplace'=>"<img src=\"\\1\" border=\"0\">");
		$repl[] = array('tag'=>"\[mail\]([^\[]+)\[/mail\]", 'tagReplace'=>"<a href=\"mailto:\\1\">\\1</a>");
		$repl[] = array('tag'=>"\[url\]([^\[]+)\[/url\]", 'tagReplace'=>"<a href=\"\\1\" target=\"_blank\">\\1</a>");
		$repl[] = array('tag'=>"\[url=\"([^\"]+)\"]([^\[]+)\[/url\]", "tagReplace'=>'<a href=\"\\1\" target=\"_blank\">\\2</a>");
		$ts = "<hr><div style=\"padding-left:30px;\"><strong>Tsiteeritud: </strong><br />\\1<br /></div><hr><br />";
		$repl[] = array('tag'=>"\[tsiteeri\]([^\[]+)\[/tsiteeri\]", 'tagReplace'=>$ts);
		$repl[] = array('tag'=>"\[tsiteeri\]([^\[]+)\[/tsiteeri\]", 'tagReplace'=>$ts);
		$repl[] = array('tag'=>"\[tsiteeri\]([^\[]+)\[/tsiteeri\]", 'tagReplace'=>$ts);
		
		foreach( $repl as $repla) {
			if( isset($repla['tagReplace']) === true ) {
				$entry = eregi_replace($repla['tag'],$repla['tagReplace'],$entry);
			} else {
				$entry = str_replace($repla['tag'], '<img src="'.DOM_MEDIA_URL . 'emoticons/' .$repla['image'].'" border="0">', $entry);
			}
		}
		return $entry;
	}
	

}
?>